<?php namespace Admin\Model;
use Hdphp\Model\Model;
//商品属性 模型
class GoodsAttr extends Model{
//	指定表名
	protected $table = 'goods_attr';
//	自动验证
	protected $validate = array(
	array('attr','required','请选择属性aid',3,3),
	array('spec','required','请选择属性value',3,3)
	);
//	添加商品属性
	public function store($attrData){
		/*if(!$this->create()){
//			实例化商品模型(将错误信息保存到商品模型)
//			$goodsModel = new \Admin\Model\Goods;
//			$goodsModel->error = $this->getError();
		
			return false;
		}*/ 
		$this->add($attrData);
		return true;
	}
//	编辑商品属性
	public function edit($gid){
//		sp($gid);exit;
//		new1、先删除货品列表的所有数据
		$goodLists = new \Admin\Model\GoodsLists;
		$goodLists->where("goods_lists_gid={$gid}")->delete();
//		1、先删除
		$this->where("goods_gid={$gid}")->delete();
//		2、再添加(属性)
		foreach(Q('post.attr',array()) as $taid => $tavalue){
			if($tavalue){
				$attrData = array(
					'type_attr_taid'=>$taid,
					'gavalue' => $tavalue,
					'goods_gid'=>$gid
				);
//				sp($attrData);
				$this->add($attrData);
			}
		}
//		3、添加规格
		$ggData = array();
		foreach(Q('post.spec',array()) as $key => $value){
			if($value){
//				sp($key);
				$ggData['type_attr_taid'] = $key;
				$ggData['goods_gid'] = $gid;
//				sp($value);
//				sp($value['price']);
				foreach($value['value'] as $k => $v){
					$ggData['gavalue'] = $v;
					$ggData['add_price'] = $value['price'][$k];
					$this->add($ggData);
				}
			}
		}
		return true;
	}
}



 ?>